package com.sudoku;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ViewFlipper;

public class SudokuActivity extends Activity implements OnClickListener {

	private ViewFlipper viewFlipper;
	private Animation filipperNext;
	private Animation filipperOutNext;
	private Animation filipperprevious;
	private Animation filipperOutPrivious;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		viewFlipper = (ViewFlipper) findViewById(R.id.viewflipper);

		filipperNext = AnimationUtils.loadAnimation(this, R.anim.flipinnext);
		filipperOutNext = AnimationUtils.loadAnimation(this, R.anim.flipoutnext);
		filipperprevious = AnimationUtils.loadAnimation(this, R.anim.flipinprevious);
		filipperOutPrivious = AnimationUtils.loadAnimation(this, R.anim.flipoutprevious);

		Button btnContinue = (Button) findViewById(R.id.continue_button);
		Button btnNewGame = (Button) findViewById(R.id.new_button);
		Button btnAbuot = (Button) findViewById(R.id.about_button);
		Button btnExit = (Button) findViewById(R.id.exit_button);

		Button btnEasy = (Button) findViewById(R.id.easy_button);
		Button btnMedium = (Button) findViewById(R.id.medium_button);
		Button btnHard = (Button) findViewById(R.id.hard_button);

		// set on click listener
		btnContinue.setOnClickListener(this);
		btnNewGame.setOnClickListener(this);
		btnAbuot.setOnClickListener(this);
		btnExit.setOnClickListener(this);

		btnEasy.setOnClickListener(this);
		btnMedium.setOnClickListener(this);
		btnHard.setOnClickListener(this);

		Typeface typeFont = Typeface.createFromAsset(getAssets(), "fonts/chicago_house.ttf");
		btnContinue.setTypeface(typeFont);
		btnNewGame.setTypeface(typeFont);
		btnAbuot.setTypeface(typeFont);
		btnExit.setTypeface(typeFont);
		
		btnEasy.setTypeface(typeFont);
		btnMedium.setTypeface(typeFont);
		btnHard.setTypeface(typeFont);
	}

	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.continue_button:

		case R.id.new_button:
			viewFlipper.setInAnimation(filipperNext);
			viewFlipper.setOutAnimation(filipperOutNext);
			viewFlipper.showNext();
			break;
		case R.id.about_button:
			break;
		case R.id.exit_button:
			finish();
			break;
		case R.id.easy_button:
			startGame(0);
			break;
		case R.id.medium_button:
			startGame(1);
			break;
		case R.id.hard_button:
			startGame(2);
			break;
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			viewFlipper.setInAnimation(filipperprevious);
			viewFlipper.setOutAnimation(filipperOutPrivious);
			viewFlipper.showPrevious();
			return true;

		default:
			return super.onKeyDown(keyCode, event);
		}

	}

	/**
	 * Create menu setting
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);

		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);

		return true;
	}

	/**
	 * Create menu item select
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);

		switch (item.getItemId()) {
		case 0:
			Intent intent = new Intent(this, Prefr.class);
			startActivity(intent);
			return true;
		}

		return false;
	}

	private void openNewGameDialog() {
		new AlertDialog.Builder(this)
				.setTitle(R.string.new_game_title)
				.setItems(R.array.difficulty,
						new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface dialog,
									int which) {
								startGame(which);

							}
						}).show();
	}

	private void startGame(int level) {

		Intent intent = new Intent(this, Game.class);
		intent.putExtra(Game.KEY_DIFFICULTY, level);
		startActivity(intent);
	}
}